package org.example.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.example.domain.SysPost;
import org.example.service.SysPostService;
import org.example.util.Result;
import org.spring.kun.common.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Api(tags = {"岗位接口"})
@RestController
@RequestMapping("sysPost")
public class SysPostController {
    @Autowired
    SysPostService sysPostService;
    /**
     *岗位管理下拉框
     */
    @ApiOperation(value = "岗位管理查询接口")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 600,message = "失败")
    })
    @RequestMapping("postsList")
    public Result postsList(){
        List<SysPost> list = sysPostService.postsList();
        return Result.success(list);
    }

    @RequestMapping("list")
    public Result list(@RequestBody SysPost sysPost, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<SysPost> list=sysPostService.list(sysPost);
        System.out.println(list);
        return Result.success(new PageInfo<>(list));
    }

    @RequestMapping("add")
    public Result add(@RequestBody SysPost sysPost){
        boolean b=sysPostService.add(sysPost);
        return b?Result.success():Result.fail();
    }

    @RequestMapping("update")
    public Result update(@RequestBody SysPost sysPost){

//        sysPostService.update()
        boolean b= sysPostService.update(sysPost);
        return b?Result.success():Result.fail();
    }
    @RequestMapping("del")
    public Result del(Long postId){

        boolean b= sysPostService.del(postId);
        return b?Result.success():Result.fail();
    }

    @RequestMapping("pishan")
    public Result pishan(Integer[] ids){

        for (int i = 0; i < ids.length; i++) {
            sysPostService.pishan(ids[i]);
        }

        return Result.success();
    }

    @RequestMapping("excel")
    public String excel(HttpServletResponse response){
        List<SysPost> list=sysPostService.lists();
        ExcelUtils.exportExcel(list,"岗位数据导出","sheet1",response);
        return "success";
    }

}
